import { isEmpty } from './base';

// 定义原始 iDoc JSON 结构中我们关心的部分的接口
interface IdoceNode {
  basic: {
    id: string;
    name: string;
    type: string;
    opacity: number;
    [key: string]: any;
  };
  bounds: {
    left: number;
    top: number;
    width: number;
    height: number;
  };
  text?: {
    styles?: any[];
  };
  slice?: {
    bitmapURL?: string;
    svgURL?: string;
  };
  children?: IdoceNode[];

  fill: {};

  [key: string]: any;
}

// 定义简化后的节点结构接口
interface SimplifiedNode {
  id: string;
  n: string; // name
  t: string; // type
  o: number; // opacity
  bounds: {
    // bounds
    x: number;
    y: number;
    w: number;
    h: number;
  };
  txt?: any; // text
  img?: string; // image url from slice
  c?: SimplifiedNode[]; // children

  [key: string]: any;
}

/**
 * 递归简化设计 JSON 节点
 * @param node - 原始 iDoc 节点
 * @returns 简化后的节点
 */
function simplifyNode(node: IdoceNode): SimplifiedNode | null {
  if (!node || !node.basic) {
    return null;
  }

  const simplified: SimplifiedNode = {
    id: node.basic.id,
    n: node.basic.name,
    t: node.basic.type,
    o: node.basic.opacity,
    bounds: {
      x: parseFloat(node.bounds.left.toFixed(2)), // 保留两位小数
      y: parseFloat(node.bounds.top.toFixed(2)),
      w: parseFloat(node.bounds.width.toFixed(2)),
      h: parseFloat(node.bounds.height.toFixed(2)),
    },
  };

  if (!isEmpty(node.fill)) {
    simplified.fill = node.fill;
  }

  if (!isEmpty(node.stroke)) {
    const stroke = {};

    Object.keys(node.stroke).forEach(key => {
      if (!isEmpty(node.stroke[key])) {
        stroke[key] = node.stroke[key];
      }
    });

    if (!isEmpty(stroke)) {
      simplified.stroke = stroke;
    }
  }

  // 处理文本节点
  if (node.text && node.text.styles && node.text.styles.length > 0) {
    simplified.txt = node.text.styles.map(style => ({
      v: style.value,
      f: {
        // font
        n: style.font.name,
        s: style.font.size,
        c: style.font.color.value,
      },
      sp: {
        // space
        lh: style.space.lineHeight,
        ls: style.space.letterSpacing,
      },
      a: style.align, // align
    }));
  }

  // 处理切片/图片
  if (node.slice && (node.slice.bitmapURL || node.slice.svgURL)) {
    simplified.img = node.slice.bitmapURL || node.slice.svgURL;
  }

  // 递归处理子节点
  if (node.children && node.children.length > 0) {
    const children = node.children.map(simplifyNode).filter(Boolean) as SimplifiedNode[];
    if (children.length > 0) {
      simplified.c = children;
    }
  }

  // 移除空的属性
  Object.keys(simplified).forEach(key => {
    const k = key as keyof SimplifiedNode;
    if (
      simplified[k] === undefined ||
      (Array.isArray(simplified[k]) && (simplified[k] as any[]).length === 0)
    ) {
      simplified[k] = null;
    }
  });

  return simplified;
}

/**
 * 简化整个 iDoc JSON 对象
 * @param designJson - 原始 iDoc JSON 对象
 * @returns 简化后的 JSON 对象
 */
export function simplifyDesignJson(designJson: any): SimplifiedNode {
  if (!designJson || !designJson.layers) {
    return designJson;
  }

  designJson.layers = simplifyNode(designJson.layers);

  return designJson;
}
